

package dictionary

import (
	"net/http"

	"github.com/micro-plat/hydra"
	"github.com/micro-plat/lib4go/errs"
	"github.com/micro-plat/lib4go/types"
)
//AuditInfoHandler 获取字典表处理服务
type DictionaryInfoHandler struct {
	insertRequiredFields []string
	updateRequiredFields []string
	pkRequiredFields []string
}

func NewDictionaryInfoHandler() *DictionaryInfoHandler {
	return &DictionaryInfoHandler{
		insertRequiredFields:[]string{},
		updateRequiredFields:[]string{},
		pkRequiredFields:[]string{"id"},
	}
}

//DeleteHandle  删除字典表数据
func (u *DictionaryInfoHandler) DeleteHandle(ctx hydra.IContext) (r interface{}) {

	ctx.Log().Info("--------删除字典表数据--------")

	ctx.Log().Info("1.检查必须字段")
	if err := ctx.Request().Check(u.pkRequiredFields...); err != nil {
		return err
	}
	
	ctx.Log().Info("2.查询数据")
	i, err := hydra.C.DB().GetRegularDB().Execute(deleteDictionaryInfo, ctx.Request().GetMap())
	if err != nil||i <= 0 {
		return errs.NewErrorf(http.StatusNotExtended, "删除数据出错:%+v", err)
	}
	return
}

//GetHandle  修改字典表数据
func (u *DictionaryInfoHandler) GetHandle(ctx hydra.IContext) (r interface{}) {

	ctx.Log().Info("--------查询字典表数据--------")

	ctx.Log().Info("1.检查必须字段")
	if err := ctx.Request().Check(u.pkRequiredFields...); err != nil {
		return err
	}
	
	ctx.Log().Info("2.查询数据")
	items, err := hydra.C.DB().GetRegularDB().Query(getDictionaryInfo, ctx.Request().GetMap())
	if err != nil {
		return errs.NewErrorf(http.StatusNotExtended, "保存数据出错:%+v", err)
	}
	return items.Get(0)
}

//PutHandle  修改字典表数据
func (u *DictionaryInfoHandler) PutHandle(ctx hydra.IContext) (r interface{}) {

	ctx.Log().Info("--------保存字典表数据--------")

	ctx.Log().Info("1.检查必须字段")
	if err := ctx.Request().Check(u.updateRequiredFields...); err != nil {
		return err
	}
	
	ctx.Log().Info("2.修改数据")
	rx, err := hydra.C.DB().GetRegularDB().Execute(updateDictionaryInfo, ctx.Request().GetMap())
	if err != nil || rx == 0 {
		return errs.NewErrorf(http.StatusNotExtended, "保存数据出错:%+v,row:%d", err,rx)
	}
	return
}

//PostHandle  保存字典表数据
func (u *DictionaryInfoHandler) PostHandle(ctx hydra.IContext) (r interface{}) {

	ctx.Log().Info("--------保存字典表数据--------")

	ctx.Log().Info("1.检查必须字段")
	if err := ctx.Request().Check(u.insertRequiredFields...); err != nil {
		return err
	}
	
	ctx.Log().Info("2.添加新数据")
	rx, err := hydra.C.DB().GetRegularDB().Execute(insertDictionaryInfo, ctx.Request().GetMap())
	if err != nil || rx == 0 {
		return errs.NewErrorf(http.StatusNotExtended, "数据出错:%+v,row:%d", err,rx)
	}
	return
}

//QueryHandle  获取字典表列表数据
func (u *DictionaryInfoHandler) QueryHandle(ctx hydra.IContext) (r interface{}) {

	ctx.Log().Info("--------获取字典表数据列表--------")

	ctx.Log().Info("1.查询数据条数")
	m := ctx.Request().GetMap()
	m["ps"] = ctx.Request().GetInt("ps", 10)
	m["offset"] = (ctx.Request().GetInt("pi", 1) - 1) * ctx.Request().GetInt("ps", 10)

	count, err := hydra.C.DB().GetRegularDB().Scalar(getDictionaryInfoListCount, m)
	if err != nil {
		return errs.NewErrorf(http.StatusNotExtended, "查询数据数量出错:%+v", err)
	}

	rcount:=types.GetInt(count)
	if  rcount== 0 {
		return map[string]interface{}{
			"count":0,
		}
	}

	ctx.Log().Info("2.查询数据列表")
	items, err := hydra.C.DB().GetRegularDB().Query(getDictionaryInfoList, m)
	if err != nil {
		return errs.NewErrorf(http.StatusNotExtended, "查询数据出错:%+v", err)
	}


	ctx.Log().Info("3.返回结果")
	return map[string]interface{}{
		"items": items,
		"count":rcount,
	}
}
